//记录查询出的所有用户信息
var areaManagerobj=new Array();
//记录点击当前行的ID 修改信息使用
var areaManagerId;

Ext.onReady(function(){
	
	areaManageriswriteidb= Ext.get('areaManageriswriteid').getValue()==1?true:false;
	
	var areaManagerNameField=new Ext.form.TextField({
		fieldLabel:'城市名称',
		labelAlign:'right'
	});
	
	var areaManagerProvinceName=new Ext.form.ComboBox({
		fieldLabel:'省 份',
		labelAlign:'right',
		width:80,
		autoScroll:true,
		store: new Ext.data.JsonStore({
        	autoLoad:true,
            url:'selectsProvince.do',
            fields:['nAreaId','sName','sCode'],
            listeners:{
           		load : function(stor,record,opts){
           			areaManagerProvinceName.setValue(record[0].data.nAreaId);
           		}
           	}
        }),
        hiddenValue:'sCode',
	    valueField:'nAreaId',
	    displayField:'sName',
        mode:'local',
        typeAhead: true,
        triggerAction: 'all'
//        selectOnFocus:true,
//        editable:false,
	});
	
	var areaManagerSearchBtn=new Ext.Button({
		text:'查询',
		handler:function(){
			areaManagerGridStore.load({
				params : {
					start : 0,
					limit : 20,
					'sName':areaManagerNameField.getValue()
				}
			});
		}
	});
	
	var areaManagerSearch=new Ext.Panel({
		height:70,
		region:'north',
		layout:'column',
		frame: true,
		border:false,
		bodyBorder:false,
		items:[
			{columnWidth:.35,layout:'form',labelAlign:'right',items:[areaManagerProvinceName]},
			{columnWidth:.35,layout:'form',labelAlign:'right',items:[areaManagerNameField]},
			{columnWidth:.15,layout:'form',labelAlign:'right',items:[areaManagerSearchBtn]}
		]
	});
	
	 areaManagerAddBtn =new Ext.Button({
		text:'添加地市',
		hidden:areaManageriswriteidb,
		handler:function(){
			showareaManagerAddForm(1);
		}
	});
	
	 areaManagerGridStore = new Ext.data.Store({
        autoLoad: false,
        proxy : new Ext.data.HttpProxy({
            url: 'searchareaManager.do'
        }),
        baseParams:{
        	sName: areaManagerNameField.getValue(),
        	nprovinceId:areaManagerProvinceName.getValue()
        },
        reader:new Ext.data.JsonReader({
             totalProperty: 'totalSize',
             root:'list'
        },[
            {name: 'nareaId',mapping:'nareaId', type: 'int'},
        	{name: 'sname',mapping:'sname', type: 'string'},
        	{name: 'scode',mapping:'scode', type: 'string'},
        	{name: 'npid',mapping:'npid', type: 'int'}
        ])
    });
    areaManagerGridStore.on("beforeload", function(thiz, options) {
		thiz.baseParams["sName"] = areaManagerNameField.getValue(); 
		thiz.baseParams["nprovinceId"] = areaManagerProvinceName.getValue();
	});
    
	var areaManagerGridBar=new Ext.PagingToolbar({
		store: areaManagerGridStore,
		displayInfo: true,
		pageSize:20,
		displayMsg : '显示第 {0} 条到 {1} 条记录，一共 {2} 条',
		emptyMsg: "没有记录可以显示",
		beforePageText:'当前页码',
		afterPageText:'共{0}页', 
		firstText:'第一页', 
		prevText:'上一页', 
		nextText:'下一页', 
		lastText:'最后一页', 
		refreshText:'刷新当前页',
		items:['-', areaManagerAddBtn]
     });
     
	areaManagerGrid = new Ext.grid.GridPanel({
        store: areaManagerGridStore,
        region:'center',
        bodyBorder:false,
        stripeRows:true,
		disableSelection: true,
		border:false,
		frame:true,
		loadMask :{msg:"数据加载中，请稍等..."},
		layout:'fit',
		viewConfig:{
			forceFit: true,
			scrollOffset:0
		},
        cm:new Ext.grid.ColumnModel([
        	new Ext.grid.RowNumberer(),
            {header: "标识", width:50, dataIndex: 'nareaId', sortable: true},
            {header: "城市",width:50, dataIndex: 'sname', sortable: true},
            {header: "城市编号",width:50, dataIndex: 'scode', sortable: true},
            {header: "操作", width:150,align:'center',sortable: true,
            	renderer:function(value, cellmeta, record, rowIndex, columnIndex, store){
			    	var areaManagerGridCrHtml='';
			    	areaManagerobj[record.data.nareaId]=record.json;
			    	if(areaManageriswriteidb==false){
			    		areaManagerGridCrHtml='<a onclick="areaManagerGridColumnsRenderUplink('+record.data.nareaId+')" href="javascript:void(0)">修改</a>';
			    	}
					return areaManagerGridCrHtml;
				}
            }
        ]),
        bbar: areaManagerGridBar
    });
    
    
	 var areaManagerMainPanel =new Ext.Viewport({
		layout:'border',
		renderTo:'areaManagerDIV',
		id:'areaManagerMainPanel',
		items:[areaManagerGrid,areaManagerSearch],
		listeners: {
	        'resize': function(viewport,adjWidth,adjHeight,rawWidth,rawHeight)  {
	          	Ext.getCmp("areaManagerMainPanel").setHeight((document.body.clientHeight - 290) * 0.67);  
			}
    	}
	});
});
	var areaManagerAddWin;
	var areaManagerAddNameField=new Ext.form.TextField({
		allowBlank:false,
		fieldLabel: '城市编号'
	});
	
	var areaManagerAddProvinceName=new Ext.form.ComboBox({
		fieldLabel:'省 份',
		labelAlign:'right',
		width:80,
		autoScroll:true,
		store: new Ext.data.JsonStore({
        	autoLoad:true,
            url:'selectsProvince.do',
            fields:['nAreaId','sName','sCode'],
            listeners:{
           		load : function(stor,record,opts){
           			areaManagerAddProvinceName.setValue(record[0].data.nAreaId);
           		}
           	}
        }),
        hiddenValue:'sCode',
	    valueField:'nAreaId',
	    displayField:'sName',
        mode:'local',
        typeAhead: true,
        triggerAction: 'all',
        editable:false
	});
	
	var areaManagerAddCityName=new Ext.form.TextField({
		fieldLabel:'城市名称',
		labelAlign:'right',
		width:80
	});
	
	//tinfo参数值1 :添加 2:修改
	function showareaManagerAddForm(tinfo,areaManagerJson) {
        if (!areaManagerAddWin) {
            areaManagerAddform = new Ext.FormPanel({
                border: false,
                labelAlign:'right',
                frame:true,
                items: [
                	areaManagerAddProvinceName,areaManagerAddCityName,
                	areaManagerAddNameField
                ],
                buttons: [{
                    text: '确 认',
                    handler: function() {
                    	
                    	if(!areaManagerAddNameField.validate()){
                    		return ;
                    	}
                    	if(this.text=='添 加'){
                    		
                    		if(areaManagerAddProvinceName.getRawValue()=='默认'){
                    			Ext.Msg.alert("信息!","请选择省份"); 
		                		return ;
                    		}
                    		
                    		if(areaManagerAddCityName.getValue()==''||areaManagerAddCityName.getRawValue()=='全部'){
		                		Ext.Msg.alert("信息!","请输入城市名称"); 
		                		return ;
		                	}
		                	
                    		Ext.Ajax.request({
								url:'addareaManager.do',
								method:'POST',
								params : {
				                   scityCode:areaManagerAddNameField.getValue(),
				                   nprovinceId:areaManagerAddProvinceName.getValue(),
				                   scityName:areaManagerAddCityName.getValue()
				                }, 
				  				success: function (response) {
	
				  					if(response.responseText=='true'){
			                            Ext.MessageBox.alert('信息!', '添加成功!');
			                            areaManagerAddform.getForm().reset();
			                            areaManagerAddWin.hide();
			                            areaManagerGridStore.reload();
				  					}
				  					else{
				  						Ext.Msg.alert("Error","添加信息失败！"); 
				  					}
				              	}, 
				  				failure: function (response) {
				                    Ext.Msg.alert("Error","错误"); 
				            	}
							});
                    	}else if(this.text=='修 改'){
                    		Ext.Ajax.request({
								url:'updateareaManager.do',
								method:'POST',
								params : {
								   nareaId : areaManagerId,
				                   scityCode:areaManagerAddNameField.getValue(),
				                   scityName:areaManagerAddCityName.getValue(),
				                   nprovinceId:areaManagerAddProvinceName.getValue()
				                }, 
				  				success: function (response) {
	
				  					if(response.responseText=='true'){
			                            Ext.MessageBox.alert('信息!', '修改成功!');
			                            areaManagerAddform.getForm().reset();
			                            areaManagerAddWin.hide();
			                            areaManagerGridStore.reload();
				  					}
				  					else{
				  						Ext.Msg.alert("Error","修改信息失败！"); 
				  					}
				              	}, 
				  				failure: function (response) {
				                    Ext.Msg.alert("Error","错误"); 
				            	}
							});
                    	}
                    }
                },{
                    text: '返 回',
                    handler: function() {
                    	areaManagerAddform.getForm().reset();
                    	areaManagerAddWin.hide();
                    }
                }]
            });

            areaManagerAddWin =new Ext.Window({
                title: '添加地市',
                closeAction: 'hide',
                width: 320,
                height: 200,
                layout: 'fit',
                resizable: true,
                modal: true,
                items: areaManagerAddform,
                listeners :{
	            	close:function(){
	            		areaManagerAddWin.getForm().reset();
	            	}
            }
            });
        }
        areaManagerAddWin.show();
        
        if(tinfo==2){
        	areaManagerAddWin.setTitle('修改地市 标识：'+areaManagerJson.nareaId);
    		areaManagerAddform.buttons[0].setText('修 改');
            areaManagerAddNameField.setValue(areaManagerJson.scode);
            areaManagerAddCityName.setValue(areaManagerJson.sname);
            areaManagerAddProvinceName.setValue(areaManagerJson.npid);
        }else{
        	areaManagerAddform.getForm().reset();
        	areaManagerAddWin.setTitle('添加地市 ');
    		areaManagerAddform.buttons[0].setText('添 加');
        }
    }
    
function areaManagerGridColumnsRenderUplink(nareaId){
	areaManagerId=nareaId;
	
	if(nareaId){
		showareaManagerAddForm(2,areaManagerobj[nareaId]);
	}
}